/*
 * CreateBandDialog.java
 *
 * Created on 16 de marzo de 2008, 10:10
 */

package org.perfectday.main.dummyengine.model;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import org.perfectday.logicengine.core.player.Player;
import org.perfectday.logicengine.model.minis.Mini;
import org.perfectday.logicengine.model.minis.MiniFactory;
import org.perfectday.logicengine.model.minis.MiniLevel;

/**
 *
 * @author  Miguel Angel Lopez Montellano ( alakat@gmail.com )
 */
public class CreateBandDialog extends javax.swing.JDialog {
    
    private MiniFactory miniFactory = MiniFactory.getInstance();
    private double bandCost;
    private Player player;
    class BandTableModel extends AbstractTableModel{
        private String[] columnNames = {"Tipo de Soldado","Level del Soldado","Coste* "};
        private List<Object[]> data = new ArrayList<Object[]>();;

        public BandTableModel() {
            
        }

        public List<Object[]> getData() {
            return data;
        }
        
        
        public BandTableModel(BandTableModel btm){
            for(Object[] obj: btm.getData())
                this.data.add(obj);
        }
        
        public int getRowCount() {
            return data.size();
        }

        public int getColumnCount() {
            return columnNames.length;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            return data.get(rowIndex)[columnIndex];
        }
        
        public void addRow(){
            data.add(new Object[3]);
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            data.get(rowIndex)[columnIndex]=aValue;
        }
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        public void remove(int i){
            data.remove(i);
        }
        
        
        public String getColumnName(int col) {
         return columnNames[col];
        }
        
        public double getTotalCost(){
            double c=0;
            for(Object[] obj:data)
                c+=((Double)obj[2]).doubleValue();
            return c;
        }
    }
    
    /** Creates new form CreateBandDialog */
    public CreateBandDialog(java.awt.Frame parent, boolean modal, double cost) {
        super(parent, modal);


        this.bandCost=cost;
        initComponents();
        for (String string : org.perfectday.logicengine.core.industry.MiniFactory.getInstance().getIndex()) {
            this.cbType.addItem(string);
        }
        this.lTotalCost.setText(lTotalCost.getText()+":"+this.bandCost);
        this.jtableBand.setModel(new BandTableModel());
        miniFactory = MiniFactory.getInstance();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        tfName = new javax.swing.JTextField();
        lTotalCost = new javax.swing.JLabel();
        bAceptar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtableBand = new javax.swing.JTable();
        cbType = new javax.swing.JComboBox();
        cbLevel = new javax.swing.JComboBox();
        lMiniCos = new javax.swing.JLabel();
        bAdd = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        lActualCost = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cbColor = new javax.swing.JComboBox();
        iaCheck = new javax.swing.JCheckBox();

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Creación de banda");

        jLabel1.setText("Nombre del jugador");

        tfName.setText("rojo");

        lTotalCost.setText("Total Point :");

        bAceptar.setText("aceptar");
        bAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAceptarActionPerformed(evt);
            }
        });

        jtableBand.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null}
            },
            new String [] {
                "Tipo de Soldado", "Nivel del Soldad", "Coste"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtableBand.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtableBandKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(jtableBand);

        cbType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbTypeActionPerformed(evt);
            }
        });

        cbLevel.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Soldado", "Soldado de primera", "Centurion", "Capitan", "Héroe", "Héroe de leyenda" }));
        cbLevel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbLevelActionPerformed(evt);
            }
        });

        lMiniCos.setText("****");

        bAdd.setText("Add");
        bAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAddActionPerformed(evt);
            }
        });

        jButton2.setText("Del");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        lActualCost.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lActualCost.setText("Coste :0");

        jLabel2.setText("Color del equipo");

        cbColor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Negro", "Rojo", "Azul", "Blanco" }));
        cbColor.setInheritsPopupMenu(true);

        iaCheck.setText("Juega la maquina");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfName, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addComponent(lTotalCost, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lActualCost, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bAceptar, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(cbColor, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(cbType, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cbLevel, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lMiniCos)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE))
                    .addComponent(iaCheck))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lTotalCost))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(iaCheck)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbType, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbLevel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lMiniCos)
                    .addComponent(bAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lActualCost)
                .addGap(4, 4, 4)
                .addComponent(bAceptar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAceptarActionPerformed
        this.player = new Player(this.tfName.getText(),
                this.iaCheck.isSelected());
        List<Mini> minis = player.getBand();
        BandTableModel bandTableModel =
                (CreateBandDialog.BandTableModel) this.jtableBand.getModel();
        for(Object[] obj:bandTableModel.getData()){
            try {
                String type = (String) obj[0];
                MiniLevel level = (MiniLevel) obj[1];
                Mini mini = miniFactory.createMini(type, level);
                minis.add(mini);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "Existe un problema con su lista de minis.\n " +
                        "Si ha realizado algún cambio consulte si estos cambios son correctos. " +
                        "\nnSi el problema persiste reinstale el sistema");
                System.exit(0);
            }
        }
        this.player.setDistingibleObjectBand(selectedColor());
        this.dispose();
}//GEN-LAST:event_bAceptarActionPerformed

    private Color selectedColor(){
        switch(this.cbColor.getSelectedIndex()){
            case 0: return Color.BLACK;
            case 1: return Color.RED;
            case 2: return Color.BLUE;
            case 3: return Color.WHITE;
            default: return Color.BLACK;
        }
    }
    private void bAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAddActionPerformed
        // TODO add your handling code here:
        int type = this.cbType.getSelectedIndex();
        int level = this.cbLevel.getSelectedIndex();
        String mType = (String) this.cbType.getSelectedItem();
        MiniLevel mLevel = MiniLevel.values()[level];
        double cost=this.miniFactory.getMiniCost(mType,mLevel); 
        BandTableModel bandTableModel =
                new BandTableModel((BandTableModel)jtableBand.getModel());
        bandTableModel.addRow();
        bandTableModel.setValueAt(mType, bandTableModel.getRowCount()-1, 0);
        bandTableModel.setValueAt(mLevel, bandTableModel.getRowCount()-1, 1);
        bandTableModel.setValueAt(cost,bandTableModel.getRowCount()-1, 2);
        this.jtableBand.setModel(bandTableModel);
        this.pack();
        if(bandTableModel.getTotalCost()>this.bandCost)
            this.bAceptar.setEnabled(false);
        this.lActualCost.setText("Cost:"+bandTableModel.getTotalCost());
}//GEN-LAST:event_bAddActionPerformed

    private void jtableBandKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtableBandKeyPressed
        
        
    }//GEN-LAST:event_jtableBandKeyPressed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try{
            int idx = this.jtableBand.getSelectedRow();

            BandTableModel bandTableModel =
                    new BandTableModel((BandTableModel)jtableBand.getModel());
            bandTableModel.remove(idx);
            this.jtableBand.setModel(bandTableModel);
            this.pack();
            if(bandTableModel.getTotalCost()<this.bandCost)
                this.bAceptar.setEnabled(true);
            this.lActualCost.setText("Cost:"+bandTableModel.getTotalCost());
        }finally{}
    }//GEN-LAST:event_jButton2ActionPerformed

    private void cbTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbTypeActionPerformed
        
        int type = this.cbType.getSelectedIndex();
        int level = this.cbLevel.getSelectedIndex();
        String mType = (String) this.cbType.getSelectedItem();
        System.out.println(mType);
        MiniLevel mLevel = MiniLevel.values()[level];
        double cost=this.miniFactory.getMiniCost(mType,mLevel); 
        lMiniCos.setText("::"+cost);
    }//GEN-LAST:event_cbTypeActionPerformed

    private void cbLevelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbLevelActionPerformed
       int type = this.cbType.getSelectedIndex();
        int level = this.cbLevel.getSelectedIndex();
        String mType = (String) this.cbType.getSelectedItem();
        MiniLevel mLevel = MiniLevel.values()[level];
        double cost=this.miniFactory.getMiniCost(mType,mLevel); 
        lMiniCos.setText("::"+cost);
    }//GEN-LAST:event_cbLevelActionPerformed

    public Player getPlayer() {
        return player;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }

    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAceptar;
    private javax.swing.JButton bAdd;
    private javax.swing.JComboBox cbColor;
    private javax.swing.JComboBox cbLevel;
    private javax.swing.JComboBox cbType;
    private javax.swing.JCheckBox iaCheck;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jtableBand;
    private javax.swing.JLabel lActualCost;
    private javax.swing.JLabel lMiniCos;
    private javax.swing.JLabel lTotalCost;
    private javax.swing.JTextField tfName;
    // End of variables declaration//GEN-END:variables
    
}
